﻿using System;
using System.IO;
using System.Management;
using System.Text;

using MyRecipeBook.Classes.Misc;
using System.Collections.Generic;

namespace MyRecipeBook.Classes
{
    public class DebugLog
    {
        public bool SaveConfig()
        {
            var bRetVal = false;

            try
            {
                var wmics = new ManagementObjectSearcher("SELECT * FROM Win32_ComputerSystem");
                var wmios = new ManagementObjectSearcher("SELECT * FROM Win32_OperatingSystem");
                var wmip = new ManagementObjectSearcher("SELECT * FROM Win32_Processor");
                var sb = new StringBuilder();
                var dt = DateTime.Now;

                sb.AppendLine(Tags.OpenTag("SystemDetailsLog", new List<Tag> { new Tag { Name = "Version", Data = "0.1.0" } }));

                sb.AppendLine(Tags.Tag(new Tag { Name = "Date", Data = string.Format("{0}/{1}/{2}", dt.Month, dt.Day, dt.Year) }, 1));
                sb.AppendLine(Tags.Tag(new Tag { Name = "Time", Data = string.Format("{0}:{1}", dt.Hour, dt.Minute) }, 1));

                sb.AppendLine(Tags.OpenTag("SystemInformation", 1));

                

                foreach (var om in wmics.Get())
                {

                    sb.AppendLine(Tags.OpenTag("BasicInformation", 2));
                    sb.AppendLine(Tags.Tag(new Tag { Name = "Manufacturer", Data = om["Manufacturer"].ToString() }, 3));
                    sb.AppendLine(Tags.Tag(new Tag { Name = "Model", Data = om["model"].ToString().Trim() }, 3));
                    sb.AppendLine(Tags.Tag(new Tag { Name = "SystemType", Data = om["SystemType"].ToString() }, 3));
                    sb.AppendLine(Tags.Tag(new Tag { Name = "Name", Data = om["Name"].ToString()}, 3));
                    sb.AppendLine(Tags.Tag(new Tag { Name = "Status", Data = om["Status"].ToString()}, 3));
                    sb.AppendLine(Tags.Tag(new Tag { Name = "Username", Data = om["Username"].ToString() }, 3));
                    sb.AppendLine(Tags.CloseTag("BasicInformation", 2));

                    sb.AppendLine(Tags.OpenTag("HardwareInformation", 2));
                    foreach (var pm in wmip.Get())
                    {
                        sb.AppendLine(Tags.OpenTag("ProcessorInfo", 3));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "Name", Data = pm["Name"].ToString().Trim() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "NumberOfProcessors", Data = om["NumberOfProcessors"].ToString() }, 4));
                        //sb.AppendLine(Tags.Tag(new Tag { Name = "NumberOfCores", Data = om["NumberOfCores"].ToString() }, 4)); //not available in xp, win 2k3
                        //sb.AppendLine(Tags.Tag(new Tag { Name = "NumberOfLogicalProcessors", Data = om["NumberOfLogicalProcessors"].ToString() }, 4));//not available in xp, win 2k3
                        sb.AppendLine(Tags.Tag(new Tag { Name = "Manufacturer", Data = pm["Manufacturer"].ToString() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "ProcessorType", Data = pm["ProcessorType"].ToString() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "Stepping", Data = pm["Stepping"].ToString() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "ProcessorId", Data = pm["ProcessorId"].ToString() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "MaxClockSpeed", Data = pm["MaxClockSpeed"].ToString() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "LoadPercentage", Data = pm["LoadPercentage"].ToString() }, 4));
                        sb.AppendLine(Tags.Tag(new Tag { Name = "Status", Data = pm["Status"].ToString() }, 4));
                        sb.AppendLine(Tags.CloseTag("ProcessorInfo", 3));
                    }

                    sb.AppendLine(Tags.Tag(new Tag { Name = "TotalPhysicalMemory", Data = om["TotalPhysicalMemory"].ToString() }, 3));
                    sb.AppendLine(Tags.CloseTag("HardwareInformation", 2));
                }              

                sb.AppendLine(Tags.CloseTag("SystemInformation", 1));
                sb.AppendLine(Tags.CloseTag("SystemDetailsLog"));

                Console.WriteLine(sb);
            }
            catch
            {
                bRetVal = false;
            }

            return bRetVal;
        }
    }
}
